home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / handlers / example5a.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  7KB  |  229 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Handlers                    Tulevagen 22       */
  8. /* File:    Example5A.c                 181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-16                                       */
  11. /* Version: 1.4                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how you can use the Pipe handler   */
  21. /* to copy (pipe) data to another program. This is the first    */
  22. /* part of the example, program A. To see how the Pipe handler  */
  23. /* work you have to start both program A and B. Once both are   */
  24. /* running can you enter some text in this (progra A's) console */
  25. /* window. When you press enter the console will be closed and  */
  26. /* the text you have entered is piped to the other program.     */
  27. /* Program B will receive the text and prints it in it's own    */
  28. /* console window.                                              */
  29. /*                                                              */
  30. /* If you want to transfer several lines of data you have to    */
  31. /* remember that the Pipe handler uses a small buffer (around   */
  32. /* 4000 bytes, 4kB), and you will only be able to collect data  */
  33. /* when the buffer has been filled, or when the file is closed. */
  34. /* In in this example I close the file once the data has been   */
  35. /* sent so it will immediately be available for program B.      */
  36.  
  37.  
  38.  
  39. /* Include the dos library definitions: */
  40. #include <dos/dos.h>
  41.  
  42. /* Now we include the necessary function prototype files:         */
  43. #include <clib/dos_protos.h>       /* General dos functions...    */
  44. #include <clib/exec_protos.h>      /* System functions...         */
  45. #include <stdio.h>                 /* Std functions [printf()...] */
  46. #include <stdlib.h>                /* Std functions [exit()...]   */
  47. #include <string.h>                /* Std functions [strlen()...] */
  48.  
  49.  
  50.  
  51. /* The maximum number of characters that we can store in */
  52. /* our small buffer (including a NULL sign at the end):  */
  53. #define MAX_LENGTH 512
  54.  
  55.  
  56.  
  57. /* Set name and version number: */
  58. UBYTE *version = "$VER: AmigaDOS/Handlers/Example5A 1.4";
  59.  
  60.  
  61.  
  62. /* Declared our own function(s): */
  63.  
  64. /* Our main function: */
  65. int main( int argc, char *argv[] );
  66.  
  67. /* Writes text to an already open file: */
  68. /* (e.g. File, Console or Pipe handler) */
  69. int print_text
  70. (
  71.   BPTR file,
  72.   STRPTR text
  73. );
  74.  
  75. /* Collects text from an already open file:*/
  76. /* (e.g. File, Console or Pipe handler)    */
  77. int collect_text
  78. (
  79.   BPTR file,
  80.   STRPTR text
  81. );
  82.  
  83.  
  84.  
  85. /* Main function: */
  86.  
  87. int main( int argc, char *argv[] )
  88. {
  89.   /* Store the number of characters used here: */
  90.   int number;
  91.  
  92.   /* Create a buffers: */
  93.   UBYTE user_input[ MAX_LENGTH ];
  94.  
  95.   /* A "BCPL" pointer to our Console window: */
  96.   BPTR my_console;
  97.  
  98.   /* A "BCPL" pointer to our Pipe handler: */
  99.   BPTR my_pipe;
  100.   
  101.  
  102.  
  103.   /* Open a Console window: (Note that we have not added a close */
  104.   /* gadget on the window, nor will it wait for the user to type */
  105.   /* Ctrl-\ before the window is closed.)                        */
  106.   my_console = 
  107.    Open( "CON:0/0/640/100/Program A", MODE_OLDFILE );
  108.   
  109.   /* Have we opened the Console successfully? */
  110.   if( my_console == NULL )
  111.   {
  112.     /* Inform the user: */
  113.     printf( "Error! Could not open the Console device!\n" );
  114.  
  115.     /* Exit with an error code: */
  116.     exit( 20 );
  117.   }
  118.  
  119.  
  120.  
  121.   /* Open the Pipe handler: (We call the pipe "UserData", and   */
  122.   /* we open the pipe handler as a new handler. Program B opens */
  123.   /* the handler as an old handler. Note that one program must  */
  124.   /* open the handler as new and the other program open the     */
  125.   /* handler as old! The order does not matter.)                */
  126.   my_pipe = 
  127.    Open( "PIPE:UserData", MODE_NEWFILE );
  128.   
  129.   /* Have we opened the Pipe handler successfully? */
  130.   if( my_pipe == NULL )
  131.   {
  132.     /* Inform the user: */
  133.     printf( "Error! Could not open the Pipe handler!\n" );
  134.  
  135.     /* Close the Console window: */
  136.     Close( my_console );
  137.  
  138.     /* Exit with an error code: */
  139.     exit( 21 );
  140.   }
  141.  
  142.  
  143.  
  144.   /* Tell the user what to do: */
  145.   print_text( my_console,
  146.     "1. Start Example7B so it will be ready to collect some text.\n" );
  147.   print_text( my_console,
  148.     "2. Type some text in this window and press ENTER (RETURN).\n" );
  149.   print_text( my_console,
  150.     "The text will be copied (piped) to the other progrm's window\n" ); 
  151.   print_text( my_console,
  152.     "were it will be printed. This window closes automatically when\n" );
  153.   print_text( my_console,
  154.     "the text has been piped.\n\nText to send: " );
  155.  
  156.   /* Collect some text from the Console window: */
  157.   number = collect_text( my_console, user_input );
  158.  
  159.   /* Send the data to the Pipe handler so the */
  160.   /* other program can collect it:            */
  161.   print_text( my_pipe, user_input );
  162.  
  163.  
  164.  
  165.   /* Close the Pipe handler: */
  166.   Close( my_pipe );
  167.  
  168.   /* Close the Console window: */
  169.   Close( my_console );
  170.  
  171.   /* Since we did not set the flag "WAIT" this console window */
  172.   /* will automatically be closed when we close the file.     */
  173.  
  174.  
  175.  
  176.   /* The End! */
  177.   exit( 0 );
  178. }
  179.  
  180.  
  181.  
  182. /* Writes text to an already opened file, and returns */
  183. /* the number of characters actualy written.          */
  184.  
  185. int print_text
  186. (
  187.   BPTR file,
  188.   STRPTR text
  189. )
  190. {
  191.   /* Store the number of characters (bytes) actualy written here: */
  192.   int characters_written;
  193.  
  194.  
  195.  
  196.   /* Write the text: */
  197.   characters_written = Write( file, text, strlen( text ) );
  198.  
  199.   /* Returns the number of characters actually written: */
  200.   return( TRUE );
  201. }
  202.  
  203.  
  204.  
  205. /* Collects text from an already opened file, and returns */
  206. /* the number of characters collected.                    */
  207.  
  208. int collect_text
  209. (
  210.   BPTR file,
  211.   STRPTR text
  212. )
  213. {
  214.   /* Store the number of characters (bytes) actualy read here: */
  215.   int characters_read;
  216.  
  217.  
  218.  
  219.   /* Collect some text: */
  220.   characters_read = Read( file, text, MAX_LENGTH - 1 );
  221.  
  222.   /* Put the NULL ('\0') sign at the end of the string: */
  223.   text[ characters_read ] = NULL;
  224.  
  225.   /* Returns the number of characters collected: */
  226.   return( characters_read );
  227. }
  228.  
  229.